package top.oylan.mp.controller.admin;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import top.oylan.mp.entity.Role;
import top.oylan.mp.entity.User;
import top.oylan.mp.entity.vo.Admin;
import top.oylan.mp.service.RoleService;
import top.oylan.mp.service.UserService;
import top.oylan.mp.utils.Pager;
import top.oylan.mp.utils.Result;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/admin/user")
public class AdminUserController {

    private Logger logger = LoggerFactory.getLogger(AdminUserController.class);

    @Resource
    UserService userService;
    @Resource
    RoleService roleService;

    @RequestMapping("/list")
    public Result index(
            @RequestParam(value = "type", defaultValue = "3") byte type,
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rowSize", defaultValue = "12") Integer rowSize
    ) {
        Result result = new Result();
        Pager<User> pager = new Pager<>();
        pager.setCurrentPage(page);
        pager.setRowSize(rowSize);
        User condition = new User();
        condition.setType(type);
        pager.setCondition(condition);
        userService.selectByPage(pager);
        result.setData(pager);
        return result;
    }

    @PostMapping("/save")
    public Result save(@RequestBody User user) {
        if (user.getId() != null && user.getId() > 0) {
            // edit
            userService.updateUser(user);
        } else {
            // add
            user.setCreateTime(new Date());
            user.setType((byte) 3);
            userService.addUser(user);
        }
        return new Result();
    }

    @PostMapping("/admin/save")
    public Result saveAdmin(@RequestBody Admin admin) {
        if (admin.getId() != null && admin.getId() != 0) {
            userService.updateAdmin(admin);
        } else {
            userService.insetAdmin(admin);
        }
        return new Result();
    }

    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    public Result delete(@RequestParam Integer id) {
        userService.deleteUser(id);
        return new Result();
    }

    @GetMapping("/admins")
    public Result admins(@RequestParam(value = "page", defaultValue = "1") Integer page,
                         @RequestParam(value = "rowSize", defaultValue = "12") Integer rowSize) {
        Result result = new Result<>();
        Pager<Admin> pager = new Pager<>();
        pager.setCurrentPage(page);
        pager.setRowSize(rowSize);
        User condition = new User();
        pager.setCondition(condition);
        List<Admin> list = userService.selectAdminByPage(pager);
        pager.setList(list);
        result.setData(pager);
        return result;
    }

    @GetMapping("/roles")
    public Result<Role> roles() {
        List<Role> list = roleService.selectAll((byte) 1);
        Result res = new Result();
        res.setData(list);
        return res;
    }
}
